﻿Public Class frmControlPeso

    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click

        If txtPeso.Text = "" Or IsNumeric(txtPeso.Text) = False Then
            MsgBox("Valores deben ser numericos", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        Dim comando As New OleDbCommand
        Dim texto As String

        comando.Connection = Conexion

        texto = "insert into control_peso (usuario, fecha, peso) values ('"
        texto &= Usuario & "', '"
        texto &= dtpFecha.Value.ToShortDateString & "', '"
        texto &= txtPeso.Text & "')"
        comando.CommandText = texto
        comando.Connection = Conexion
        Try
            comando.ExecuteNonQuery()
            MsgBox("Registro Agregado", MsgBoxStyle.Information)
            CargarTabla()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Sub cargartabla()

        Dim data As New DataSet
        Dim adaptador As New OleDb.OleDbDataAdapter
        Dim comando As New OleDb.OleDbCommand
        Dim fecha As String
        Dim i As Integer

        fecha = DateAdd(DateInterval.Day, -30, Now).ToShortDateString

        comando.Connection = Conexion

        comando.CommandText = "Select * from control_peso where Fecha>#" & fecha & "# order by fecha"
        adaptador.SelectCommand = comando
        adaptador.Fill(data, "tabla")

        'dgvMaquinas.Rows.Clear()
        dgvHistorial.DataSource = data
        dgvHistorial.DataMember = "tabla"
        dgvHistorial.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dgvHistorial.ReadOnly = True
        dgvHistorial.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dgvHistorial.Columns(0).Visible = False
        dgvHistorial.Columns(1).Visible = False
        dgvHistorial.Refresh()

        Graficar()

    End Sub

    Private Sub frmControlPeso_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtpFecha.MaxDate = Now
        cargartabla()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Close()
    End Sub
    Sub Graficar()
        Chart1.Series(0).Points.Clear()
        Chart1.Series(0).Name = "Peso"
        For i = 0 To dgvHistorial.RowCount - 2
            Chart1.Series(0).Points.AddXY(dgvHistorial.Rows(i).Cells(2).Value, dgvHistorial.Rows(i).Cells(3).Value)
        Next

    End Sub
End Class